Thread: &, &&, |, || operations question

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    23

    &, &&, |, || operations question

    Okay so I know there are four operations, but I dont get how these work. Can someone please explain how these work in binary terms and decimal terms?

    Well for binary terms, I kind of get it but not too sure, but for decimal how do i noe 6|9 = 15. PLEASE EXPLAIN!!!

    Thanks in advance

  2. #2
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    The & is "bitwise and". That is, if the bits, when lined up, are both 1's, the return is 1, otherwise it's 0. The && is used for comparisons meaning "and". The | is "bitwise or". That is, if the bits are both 0's when lined up, it'll return 0, otherwise, it'll return 1. The || is used for comparisons and means "or". There's also the ^, which is "bitwise xor". If the bits are not the same, 1 is returned, 0 if otherwise.

    Examples:
    Code:
    Bitwise and:
    01001100
    00101001
    --------
    00001000
    
    Bitwise or:
    01001100
    00101001
    --------
    01101101
    
    Bitwise xor:
    01001100
    00101001
    --------
    01100101
    
    Comparison and:
    if ((AndExample > 5) && (AndExample <= 10))
    {
    	... // done only if both conditions are true
    }
    
    if ((OrExample <= 6) || (OrExample > 8))
    {
    	... // done if either condition is true
    }
    
    if ((Complex > 8) && ((Complex < 12) || (Complex == 15)))
    {
    	... // done if the first condition is true and one of the other two are
    }
    These "bitwise" operators are used to manipulate the bits of a variable, almost certainly integer types only (as opposed to floating point ones where I doubt it works with those). This is common C syntax.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    23
    okay i get the binary part. what bout changing it to decimal? how would i know what number wud it return?

  4. #4
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    for decimal also its the same.
    what i do is for eg
    5&3
    just convert both of them to binary and do as ulillillia told and after the result convert it to decimal.
    so 5&3=1
    same is for bitwise OR.
    so 5|3=7
    for && and || there is nothing to do with decimal or binary,they just check their operands and according to their being true or false give the required result.expressions involving && or|| are evaluated left to right, and evaluation stops as soon as truth or falsehood is known.
    Last edited by BEN10; 04-07-2009 at 02:03 AM.

  5. #5
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    As a side note, the | and ^ operators are commonly used to set flags.

    Code:
    FlagsExample = Flag | AnotherFlag | OneMoreFlag; // sets these flags directly
    FlagsExample ^= AnotherFlag; // toggles only this flag
    The top is used to set flags directly, useful on initiation. The bottom is used to toggle a flag on and off.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And to turn off a flag, you would use:
    Code:
    FlagsExample &= ~AnotherFlag;
    Which also introduces a new operator: ~ - which means "invert" the bits.

    Of course, ALL of the above code assumes that flags are 1 << n (or 2 to the power of n) so that each flag is a single bit.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by dbzx View Post
    okay i get the binary part. what bout changing it to decimal? how would i know what number wud it return?
    For signed types, you don't -- it's implementation defined.

    And must you use broken English? Is "would" really that much more effort than "wud"?

    > The || is used for comparisons and means "or"
    Ie, "boolean or" or "logical or". FYI "or" is a keyword in C++ which means the same thing, although I see no logical reason why it was introduced (another C -> C++ issue).
    Last edited by zacs7; 04-07-2009 at 07:29 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by zacs7
    FYI "or" is a keyword in C++ which means the same thing, although I see no logical reason why it was introduced (another C -> C++ issue).
    It is available as a macro in C via <iso646.h>.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by zacs7 View Post
    Ie, "boolean or" or "logical or". FYI "or" is a keyword in C++ which means the same thing, although I see no logical reason why it was introduced (another C -> C++ issue).
    It was introduced in C++ because (in the old days, using a 7-bit "ASCII" [1] table)
    Code:
    |, [, ], \, {, }
    are actuallu letters like ä, å, ö, Æ, Ö, and such in many European languages that use letters beyond A-Z. Since C++ is a language with new keywords in the first place, it also allows the or, and, etc keywords to be used, to reduce the number of symbols that look different in these character sets.

    [1] Quotes to indicate that it is obviously NOT ASCII if you do not have those symbols that are part of ASCII - but it's in most parts ASCII, with some small changes on the rarely used braces and brackets - however, C and C++ uses these quite a lot.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    23
    Code:
    just convert both of them to binary and do as ulillillia told and after the result convert it to decimal.
    sorry for more questions but how would i know what the binary of each number is? and how would i convert it to decimal?

  11. #11
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Location
    Finland
    Posts
    194
    you can calculate values using quite simple math. Search binary to decimal base with google. Or then use calculator as rest of us

  12. #12
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    I can do the math in my head... to a certain point. My limit is 21 bits. It shouldn't be too difficult to convert binary to decimal and back.

    For the top of my example numbers, they are, without using a calculator:

    0*128+1*64+0*32+0*16+1*8+1*4+0*2+0*1 = 64+16+8 = 88.

    For the random number of 157, to go backwards, ask yourself, can you take 128 out of it (that is, is it greater than 128)? Since you can, the leftmost bit is a 1. Subtract: 157-128 = 29 (yes, I can subtract that in my head). Now, can you take 64 away from this? Nope - that bit is 0. Can you take 32 away? Again, that bit is zero as well. You can take 16 away so that bit is 1. Subtract again - 29-16=13. You can then take 8 out (5 left), 4 (1 left), not 2, but 1 (0 left). This gives 10011101 as your binary value, which is 0x9D in hexadecimal notation.

    Try it - convert the number 10100010 into decimal. Now convert 56 into binary.
    High elevation is the best elevation. The higher, the better the view!
    My computer: XP Pro SP3, 3.4 GHz i7-2600K CPU (OC'd to 4 GHz), 4 GB DDR3 RAM, X-Fi Platinum sound, GeForce 460, 1920x1440 resolution, 1250 GB HDD space, Visual C++ 2008 Express

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Homework help
    By mkdl750 in forum C Programming
    Replies: 45
    Last Post: 07-17-2008, 09:44 PM
  2. !(1 && !(0 || 1)) Help
    By scope in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 11:19 PM
  3. Evaluate !(1 && !(0 || 1)).
    By Grumpy_Old_Man in forum C++ Programming
    Replies: 7
    Last Post: 08-18-2003, 01:28 PM
  4. Shorter notation?
    By GaPe in forum C Programming
    Replies: 2
    Last Post: 06-14-2003, 11:35 AM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM